home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Cheat II / original / Show_help.c < prev    next >
Text File  |  1995-02-02  |  28KB  |  1,019 lines

  1. #define A4_PROJECT    0    /* Set to 1 for a code resource (DA, cdev, etc.) */
  2. #define USE_PICTS    1    /* 1 if PICTs are to be displayed, 0 for text only */
  3. #define ONE_RESOURCE 1    /* 1 for Get1Resource, 0 for GetResource, etc. */
  4.  
  5.  
  6. /*
  7.     Show_help by James W. Walker, June 1991
  8.     This code is freely usable.
  9.     
  10.     e-mail:
  11.         Internet            76367.2271@compuserve.com
  12.         CIS                    76367,2271
  13.         America Online        JWWalker
  14.     
  15.     This code displays scrolling text in a dialog box.  The text comes
  16.     from TEXT/styl resources, which can be created with ResEdit 2.1.
  17.     Pictures can be included in the text using the same scheme as
  18.     TeachText: Each option-space character indicates where the top
  19.     edge of a picture should go, and pictures are centered horizontally.
  20.     Pictures come from consecutively-numbered PICT resources.  The text
  21.     cannot be edited, but one can select text and copy it to the
  22.     clipboard using command-C, or save it as a TeachText file.
  23.     
  24.     The fact that our window is not resizeable has its advantages.  We
  25.     can compute where the pictures go, once and for all, except to take
  26.     vertical scrolling into account.
  27.     
  28.     A popup menu can be used to jump to "bookmarks", which are indicated
  29.     by tab characters at ends of lines.
  30.     
  31.     Prototype:
  32.     
  33.     void Show_help( short help_dlog_id, short help_text_id,
  34.         short base_pict_id, StringPtr default_filename,
  35.         StringPtr default_menuname );
  36.     
  37.     help_dlog_id  is the resource ID of the DLOG resource.  The dialog
  38.                   should have an OK button as item 1 and a userItem, to
  39.                   display the text, in item 2.
  40.     
  41.     help_text_id is the resource ID of the TEXT and styl resources.
  42.     
  43.     base_pict_id is the resource ID of the first PICT resource.
  44.  
  45.     default_filename is the filename initially presented in the Save dialog
  46.                   when the user saves the help text as TeachText.
  47.     
  48.     default_menuname is the menu item used for a line that consists of
  49.                   a tab and nothing else.
  50. */
  51.  
  52.  
  53. #ifndef NIL
  54. #define        NIL        0L
  55. #endif
  56.  
  57. #if A4_PROJECT
  58. #include <SetUpA4.h>
  59. #endif
  60.  
  61. #if ONE_RESOURCE
  62. #define GetResource            Get1Resource
  63. #define CountResources        Count1Resources
  64. #endif
  65.  
  66. #define CHECKPOINTS 0
  67. #define ASSERTIONS    1
  68.  
  69. #if ASSERTIONS
  70. #define        ASSERT(x,y)        if (!(x)) {DebugStr("\p" y);}
  71. #else
  72. #define        ASSERT(x,y)
  73. #endif
  74.  
  75. #if CHECKPOINTS
  76. #define CKPT(x)        DebugStr( "\p" x );
  77. #else
  78. #define CKPT(x)
  79. #endif
  80.  
  81. enum {
  82.     c_OK = 1,    /* OK button */
  83.     c_help,        /* userItem for our help display */
  84.     c_save,        /* Button to save as TeachText */
  85.     c_menu        /* userItem for popup menu */
  86. };
  87.  
  88. #define        SCROLLBAR_WIDTH    16
  89. #define        TEXT_INSET        4
  90.  
  91. typedef struct {
  92.     Rect        bounds;
  93.     PicHandle    pict;
  94. } pict_info;
  95.  
  96. typedef struct {
  97.     DialogRecord    dialog;
  98.     ControlHandle    scrollbar;
  99. #if USE_PICTS
  100.     Boolean            high_defer_flag;
  101.     Rect            high_rect[3];
  102.     short            high_waiting;
  103.     short            pict_count;    /* how many pictures */
  104.     pict_info        *pict_data;    /* pointer to an array */
  105. #endif
  106. }    help_record, *help_ptr;
  107.  
  108. /* private global variables */
  109. static    RgnHandle    save_clip;
  110. static    CursHandle    watch_cursor, ibeam_cursor;
  111.  
  112. /* Prototypes of public routines */
  113. void Show_help( short help_dlog_id, short help_text_id,
  114.     short base_pict_id, StringPtr default_filename,
  115.     StringPtr default_menuname );
  116. void Flash_button( DialogPtr the_dialog, short item_number );
  117.  
  118. /* Prototypes of private routines
  119.  */
  120. static pascal Boolean Help_filter( DialogPtr dialog,
  121.     EventRecord    *event, short *itemHit);
  122. static pascal void  userItem_proc( WindowPtr the_window, short item_num );
  123. static pascal void  Menu_userItem_proc( WindowPtr the_window, short item_num );
  124. static pascal void Scroll_text( ControlHandle the_bar, int part_code );
  125. static pascal Auto_scroll( void );
  126. static void Handle_scroll( DialogPtr dialog,
  127.     short the_part, Point local_point );
  128. static void Adjust_text( DialogPtr dialog );
  129. static void Save_text( TEHandle the_text, short base_pict_id,
  130.     StringPtr default_filename );
  131. static void Topic_menu( DialogPtr dlog, MenuHandle help_popup );
  132. static MenuHandle Build_popup( TEHandle    the_text, StringPtr default_menuname );
  133.  
  134. #if USE_PICTS
  135. static void Find_pictures( DialogPtr dlog, short first_pict_id );
  136. static void Draw_picts( WindowPtr the_window, Rect *update_rect );
  137. static pascal void High_hook( Rect *high_rect );
  138. static void High_hook_glue( void );
  139. static void Do_deferred_hilites( help_ptr  hptr, Rect *update_rect );
  140. #endif
  141.  
  142. /* ------------------------- Show_help --------------------------------- */
  143. void Show_help( short help_dlog_id, short help_text_id,
  144.     short base_pict_id, StringPtr default_filename,
  145.     StringPtr default_menuname )
  146. {
  147.     register    DialogPtr    dptr;
  148.     register    TEHandle    the_text;
  149.     short        itype, ihit;
  150.     Handle        item_h;
  151.     Rect        help_item_box, box;
  152.     Handle            help_TEXT;
  153.     StScrpHandle    help_styl;
  154.     GrafPtr        save_port;
  155.     Rect        dest, view;
  156.     ControlHandle    the_bar;
  157.     short        max_scroll, nLines;
  158.     Point        place;
  159.     MenuHandle    help_popup;
  160.     
  161.     watch_cursor = GetCursor( watchCursor );
  162.     SetCursor( *watch_cursor );
  163.     ibeam_cursor = GetCursor( iBeamCursor );
  164.     dptr = (DialogPtr) NewPtr( sizeof(help_record) );
  165.     dptr = GetNewDialog( help_dlog_id, (DialogPeek) dptr, (WindowPtr)-1L );
  166.     ASSERT( dptr != NIL, "Failed GetNewDialog" );
  167.     GetPort( &save_port );
  168.     SetPort( dptr );
  169.     
  170.     help_TEXT = GetResource( 'TEXT', help_text_id );
  171.     if (help_TEXT == NIL)
  172.     {
  173.         ASSERT(false, "Failed to find help TEXT resource" );
  174.         SysBeep(1);
  175.         goto getout;
  176.     }
  177.     help_styl = (StScrpHandle) GetResource( 'styl', help_text_id );
  178.     if (help_styl == NIL)
  179.     {
  180.         DisposHandle( help_TEXT );
  181.         ASSERT( false, "Failed to find styl resource" );
  182.         SysBeep(1);
  183.         goto getout;
  184.     }
  185.     HLock( help_TEXT );
  186.     
  187.     GetDItem( dptr, c_help, &itype, &item_h, &help_item_box );
  188.     SetDItem( dptr, c_help, itype, (Handle) userItem_proc, &help_item_box );
  189.     view = help_item_box;
  190.     InsetRect( &view, 1, 1 );
  191.     view.right -= SCROLLBAR_WIDTH;
  192.     dest = view;
  193.     InsetRect( &dest, TEXT_INSET, 0 );
  194.     the_text = TEStylNew( &dest, &view );
  195.     ASSERT( the_text != NIL, "Failed TEStylNew." );
  196.     
  197.     TEStylInsert( *help_TEXT, GetHandleSize(help_TEXT),
  198.         help_styl, the_text );
  199.     TECalText( the_text );    /* Maybe not needed, but can't hurt. */
  200.     TEActivate( the_text );
  201.     ReleaseResource( (Handle) help_styl );
  202.     ReleaseResource( help_TEXT );
  203.     nLines = (**the_text).nLines;
  204.     SetWRefCon( dptr, (long)the_text );
  205.     max_scroll = TEGetHeight( (long) nLines, 1L, the_text )
  206.         - (view.bottom - view.top);
  207.     
  208.     help_item_box.left = help_item_box.right - SCROLLBAR_WIDTH;
  209.     the_bar = NewControl( dptr, &help_item_box, "\p", true,
  210.         0, 0, max_scroll, scrollBarProc, NIL );
  211.     ASSERT( the_bar != NIL, "Failed NewControl for scroll bar." );
  212.     ((help_ptr) dptr)->scrollbar = the_bar;
  213.  
  214. #if USE_PICTS
  215.     Find_pictures( dptr, base_pict_id );
  216. #endif
  217.     help_popup = Build_popup( the_text, default_menuname );
  218.  
  219.     TEAutoView( TRUE, the_text );    /* Permit auto-scrolling */
  220.     CKPT( "Installing ClikLoop" );
  221.     (**the_text).clikLoop = (ProcPtr)Auto_scroll;
  222. #if USE_PICTS
  223.     (**the_text).highHook = (ProcPtr) High_hook_glue;
  224.     ((help_ptr)dptr)->high_defer_flag = false;
  225. #endif
  226.  
  227.     GetDItem( dptr, c_menu, &itype, &item_h, &box );
  228.     SetDItem( dptr, c_menu, itype, (Handle) Menu_userItem_proc, &box );
  229.     
  230.     save_clip = NewRgn();    /* Used in Draw_picts */
  231.     ShowWindow( dptr );
  232. #if A4_PROJECT
  233.     RememberA4();
  234. #endif
  235.     InitCursor();
  236.  
  237.     do {
  238.         ModalDialog( (ProcPtr) Help_filter, &ihit );
  239.         if (ihit == c_save)
  240.             Save_text( the_text, base_pict_id, default_filename );
  241.         else if (ihit == c_menu)
  242.             Topic_menu( dptr, help_popup );
  243.     } while (ihit != c_OK);
  244.     
  245.     
  246.     DisposeRgn( save_clip );
  247. #if USE_PICTS
  248.     DisposPtr( (Ptr) ((help_ptr)dptr)->pict_data );
  249. #endif
  250.     TEDispose( the_text );
  251. getout:
  252.     DisposDialog( dptr );
  253.     DisposeMenu( help_popup );
  254.     SetPort( save_port );
  255. }
  256.  
  257. /* --------------------------- Build_popup ------------------------- */
  258. /*
  259.     Build a popup menu of the sections of the help text.  We scan for
  260.     tab characters.  The text between the tab character and the preceding
  261.     line break will be a menu item, unless it is the null string; then we
  262.     use the default menu name that was passed to Show_help.
  263. */
  264. static MenuHandle Build_popup( TEHandle    the_text, StringPtr default_menuname )
  265. {
  266.     MenuHandle    popup;
  267.     short        menu_id;
  268.     SignedByte    text_state;
  269.     Handle        text_h;    /* handle to just the text */
  270.     Str255        menu_data;
  271.     char        *text;    /* pointer to the help text */
  272.     register short        scan, line_start;
  273.     short        text_size, title_length;
  274.     
  275.     /* Find an unused menu ID */
  276.     menu_id = 1300; /* no particular reason */
  277.     while (GetMHandle(menu_id))
  278.         ++menu_id;
  279.     
  280.     popup = NewMenu( menu_id, "\p" );
  281.  
  282.     text_h = (**the_text).hText;
  283.     text_state = HGetState( text_h );
  284.     HLock( text_h );
  285.     text = *text_h;
  286.     text_size = (short) GetHandleSize( text_h );
  287.     line_start = 0;
  288.     for (scan = 0; scan < text_size; scan++ )
  289.     {
  290.         if (text[scan] == '\r')
  291.         {
  292.             line_start = scan + 1;
  293.         }
  294.         else if (text[scan] == '\t')
  295.         {
  296.             title_length = scan - line_start;
  297.             if (title_length == 0)
  298.                 AppendMenu( popup, default_menuname );
  299.             else if (title_length <= 255)
  300.             {
  301.                 menu_data[0] = title_length;
  302.                 BlockMove( &text[line_start], &menu_data[1], title_length );
  303.                 AppendMenu( popup, menu_data );
  304.             }
  305.         }
  306.     }
  307.     
  308.     HSetState( text_h, text_state );
  309.     return popup;
  310. }
  311.  
  312. /* ------------------------- Topic_menu ------------------------ */
  313. /*
  314.     This routine is called when the menu title is clicked.
  315.     It pops up the menu and scrolls to the indicated tab character.
  316. */
  317. static void Topic_menu( DialogPtr dptr, MenuHandle menu )
  318. {
  319.     short            menu_id;
  320.     Handle            item_h;
  321.     short            itype;
  322.     Rect            box;
  323.     Point            where;
  324.     long            menu_return;
  325.     short            menu_choice;
  326.     ControlHandle    bar;
  327.     char            tab_char[2] = "\t";
  328.     register short        i;
  329.     register TEHandle    the_text;
  330.     Handle            text_h;
  331.     SignedByte        text_state;
  332.     register long        offset;
  333.     TextStyle        what_style;
  334.     short            line_height, font_ascent;
  335.     
  336.     if (menu == NIL) return;
  337.     InsertMenu( menu, -1 );
  338.     GetDItem( dptr, c_menu, &itype, &item_h, &box );
  339.     where.h = box.left;
  340.     where.v = box.bottom;
  341.     LocalToGlobal( &where );
  342.     InvertRect( &box );
  343.     menu_return = PopUpMenuSelect( menu, where.v, where.h, 0 );
  344.     InvertRect( &box );
  345.     if (HiWord(menu_return))    /* Something selected */
  346.     {
  347.         menu_choice = LoWord( menu_return );
  348.         bar = ((help_ptr)dptr)->scrollbar;
  349.         the_text = (TEHandle) GetWRefCon( dptr );
  350.         text_h = (**the_text).hText;
  351.         text_state = HGetState( text_h );
  352.         HLock( text_h );
  353.         
  354.         /* Find tab character number menu_choice */
  355.         offset = -1L;
  356.         for (i = 1; i <= menu_choice; ++i)
  357.         {
  358.             ++offset; /* so we don't find the same thing twice */
  359.             offset = Munger(text_h, offset, tab_char, 1L, NIL, NIL);
  360.         }
  361.  
  362.         where = TEGetPoint( (short)offset, the_text );
  363.         TEGetStyle( (short)offset, &what_style, &line_height,
  364.             &font_ascent, the_text );
  365.         where.v -= line_height;    /* align to TOP of tab */
  366.         /*
  367.             Now where.v is in local coordinates.
  368.         */
  369.         where.v -= (**the_text).destRect.top;
  370.         SetCtlValue( bar,  where.v );
  371.         HSetState( text_h, text_state );
  372.         
  373.         Adjust_text( dptr );
  374.     }
  375.     menu_id = (**menu).menuID;
  376.     DeleteMenu( menu_id );
  377. }
  378.  
  379. /* ------------------------- Save_text ------------------------ */
  380. /*
  381.     This is called when the user clicks on the "Save as TeachText"
  382.     button.
  383. */
  384. static void Save_text( TEHandle the_text, short base_pict_id,
  385.     StringPtr default_filename )
  386. {
  387.     Point    where;
  388.     static Str255    prompt = "\pName of TeachText file:";
  389.     SFReply        reply;
  390.     OSErr        err;
  391.     short        data_refnum, res_refnum, old_resfile;
  392.     Handle        text_data;
  393.     SignedByte    state;
  394.     long        count;
  395. #if USE_PICTS
  396.     register short        num_picts, pict_id;
  397.     Handle        old_pict, new_pict;
  398. #endif
  399.     
  400.     where.h = where.v = 100;
  401.     SFPutFile( where, prompt, default_filename, NIL, &reply );
  402.     if (reply.good)
  403.     {
  404.         old_resfile = CurResFile();
  405. #if USE_PICTS
  406.         num_picts = CountResources( 'PICT' );
  407. #endif
  408.         
  409.         err = FSDelete( reply.fName, reply.vRefNum );
  410.         err = Create( reply.fName, reply.vRefNum, 'ttxt', 'ttro' );
  411.         err = FSOpen( reply.fName, reply.vRefNum, &data_refnum );
  412.         text_data = (**the_text).hText;
  413.         state = HGetState(text_data );
  414.         HLock( text_data );
  415.         count = GetHandleSize( text_data );
  416.         err = FSWrite( data_refnum, &count, *text_data );
  417.         err = FSClose( data_refnum );
  418.         HSetState( text_data, state );
  419.  
  420. #if USE_PICTS
  421.         if (num_picts > 0)
  422.         {
  423.             err = SetVol( NIL, reply.vRefNum );
  424.             CreateResFile( reply.fName );
  425.             err = ResError();
  426.             res_refnum = OpenResFile( reply.fName );
  427.             err = ResError();
  428.             for (pict_id = base_pict_id;
  429.                 pict_id < base_pict_id + num_picts; ++pict_id )
  430.             {
  431.                 UseResFile( old_resfile );
  432.                 old_pict = GetResource( 'PICT', pict_id );
  433.                 if (old_pict == NIL)
  434.                     break;
  435.                 new_pict = old_pict;
  436.                 err = HandToHand( &new_pict );
  437.                 UseResFile( res_refnum );
  438.                 AddResource( new_pict, 'PICT',
  439.                     pict_id - base_pict_id + 1000, "\p" );
  440.                 err = ResError();
  441.             }
  442.             CloseResFile( res_refnum );
  443.             err = FlushVol( NIL, reply.vRefNum );
  444.         }
  445. #endif
  446.     }
  447. }
  448.  
  449. #if USE_PICTS
  450. /* ------------------------- High_hook_glue ------------------------ */
  451. static void High_hook_glue( void )
  452. {
  453.     asm {
  454.         move.L    (SP)+, A0    ; get address of rectangle
  455.         movem.L    A2-A5/D3-D7, -(SP)    ; save registers
  456.         move.L    A0, -(SP)
  457.     }
  458.     CKPT( "High_hook_glue" );
  459.     asm {
  460.         JSR        High_hook
  461.         movem.L    (SP)+, A2-A5/D3-D7    ; restore registers
  462.         RTS
  463.     }
  464. }
  465.  
  466. /* ------------------------- High_hook -------------------------- */
  467. /*
  468.     This deferred highlighting scheme is used to ensure that highlighting
  469.     will be done after any pictures have been drawn, not before.  To do
  470.     otherwise can cause pictures to be incorrectly highlighted during
  471.     auto-scrolling.  This effect can be seen in TeachText.
  472. */
  473. static pascal void High_hook( Rect *the_rect )
  474. {
  475.     register help_ptr    front;
  476.     
  477.     CKPT( "High_hook");
  478.     front = (help_ptr) FrontWindow();
  479.     if ( (front == NIL) ||
  480.         (GetPtrSize((Ptr)front) != sizeof(help_record)) )
  481.         return;
  482.     if (!front->high_defer_flag)
  483.         InvertRect( the_rect );
  484.     else
  485.     {
  486.         if ( front->high_waiting >= 3 )
  487.             DebugStr("\pHighlight overflow");
  488.         else
  489.         {
  490.             front->high_rect[ front->high_waiting ]
  491.                 = *the_rect;
  492.             front->high_waiting++;
  493.         }
  494.     }
  495. }
  496. #endif /* USE_PICTS */
  497.  
  498. /* ------------------------- Auto_scroll ----------------------------- */
  499. /*
  500.     This is a ClikLoop routine, called repeatedly by TEClick when the
  501.     mouse is down.
  502. */
  503. static pascal Auto_scroll()
  504. {
  505.     register    WindowPtr    the_display;
  506.     register    ControlHandle    the_bar;
  507.     Point                    mouse_point;
  508.     Rect                    view_rect;
  509.     register     TEHandle    the_text;
  510.     
  511.     asm {
  512.         movem.l        a1-a5/d1-d7, -(SP)
  513.     }
  514.     CKPT( "Auto_scroll");
  515.     the_display = FrontWindow();
  516.     if ( (the_display != NIL) &&
  517.         (GetPtrSize((Ptr)the_display) == sizeof(help_record)) )
  518.     {
  519.         the_text = (TEHandle) GetWRefCon( the_display );
  520.         the_bar = ((help_ptr) the_display)->scrollbar;
  521.         
  522.         GetMouse( &mouse_point );
  523.         view_rect = (**the_text).viewRect;
  524.         if (mouse_point.v < view_rect.top)
  525.             Scroll_text( the_bar, inUpButton );
  526.         else if (mouse_point.v > view_rect.bottom)
  527.             Scroll_text( the_bar, inDownButton );
  528.     }
  529.     asm {
  530.         movem.L        (SP)+, a1-a5/d1-d7
  531.         moveQ        #1, D0
  532.     }
  533. }
  534.  
  535. #if USE_PICTS
  536. /* ------------------------- Draw_picts --------------------------------- */
  537. /*
  538.     Called by Adjust_text and userItem_proc to draw pictures.
  539. */
  540. static void Draw_picts( WindowPtr the_window, Rect *update_rect )
  541. {
  542.     register TEHandle    the_text;
  543.     register short        pict_count, pict_index;
  544.     PicHandle    the_pict;
  545.     short         v_offset;
  546.     Rect        pict_loc, dummy;
  547.     
  548.     CKPT( "Draw_picts");
  549.     the_text = (TEHandle) GetWRefCon( the_window );
  550.     v_offset = (**the_text).destRect.top - (**the_text).viewRect.top
  551.         - TEXT_INSET;
  552.     pict_count = ((help_ptr) the_window)->pict_count;
  553.     for (pict_index = 0; pict_index < pict_count; pict_index++)
  554.     {
  555.         pict_loc = ((help_ptr) the_window)->pict_data[pict_index].bounds;
  556.         OffsetRect( &pict_loc, 0, v_offset );
  557.         if (!SectRect( &pict_loc, update_rect, &dummy ))
  558.             continue;
  559.         the_pict = ((help_ptr) the_window)->pict_data[pict_index].pict;
  560.         LoadResource( (Handle) the_pict );
  561.         HLock( (Handle) the_pict );
  562.         GetClip( save_clip );
  563.         ClipRect( update_rect );
  564.         DrawPicture( the_pict, &pict_loc );
  565.         SetClip( save_clip );
  566.         HUnlock( (Handle) the_pict );
  567.     }
  568. }
  569.  
  570. /* ---------------------- Find_pictures ---------------------------- */
  571. static void Find_pictures( DialogPtr dlog, short first_pict_id )
  572. {
  573.     register TEHandle    the_text;
  574.     Handle        text_h;
  575.     SignedByte    text_state;
  576.     register long        offset;
  577.     long        text_size;
  578.     short        num_picts;
  579.     register short        which_pict;
  580.     char        option_space[2] = "\xCA";
  581.     pict_info    *pict;
  582.     Point        place;
  583.     short        line_height, font_ascent;
  584.     TextStyle    what_style;
  585.     
  586.     CKPT( "Find_pictures");
  587.     the_text = (TEHandle) GetWRefCon( dlog );
  588.     text_h = (**the_text).hText;
  589.     text_state = HGetState( text_h );
  590.     HLock( text_h );
  591.     
  592.     /* Count option-space characters in the text. */
  593.     text_size = GetHandleSize( text_h );
  594.     offset = 0L;
  595.     num_picts = 0;
  596.     offset = Munger(text_h, offset, option_space, 1L, NIL, NIL );
  597.     while ( (offset >= 0L) && (offset <= text_size) )
  598.     {
  599.         num_picts++;
  600.         offset++;
  601.         offset = Munger(text_h, offset, option_space, 1L, NIL, NIL );
  602.         
  603.     }
  604.     
  605.     /* Allocate storage for an array of picture bounds. */
  606.     pict = (pict_info *) NewPtr( sizeof(pict_info) * num_picts );
  607.     ((help_ptr)dlog)->pict_data = pict;
  608.     
  609.     /*
  610.         Initialize the picture info.  For each picture we record the
  611.         picture handle and its rectangle, in unscrolled window
  612.         coordinates.
  613.     */
  614.     offset = 0L;
  615.     for (which_pict = 0; which_pict < num_picts; which_pict++)
  616.     {
  617.         pict[which_pict].pict = (PicHandle) GetResource( 'PICT',
  618.             first_pict_id + which_pict );
  619.         if ( pict[which_pict].pict == NIL )
  620.             break;
  621.         offset = Munger(text_h, offset, option_space, 1L, NIL, NIL );
  622.         place = TEGetPoint( (short)offset, the_text );
  623.         TEGetStyle( offset, &what_style, &line_height,
  624.             &font_ascent, the_text );
  625.         place.v -= line_height;    /* align picture with TOP of option-space */
  626.         offset++;
  627.         pict[which_pict].bounds = (**pict[which_pict].pict).picFrame;
  628.         OffsetRect( &pict[which_pict].bounds,
  629.             ( ((**the_text).destRect.right + (**the_text).destRect.left) -
  630.             (pict[which_pict].bounds.right + pict[which_pict].bounds.left)
  631.             ) / 2,
  632.             - pict[which_pict].bounds.top + place.v );
  633.     }
  634.     ((help_ptr)dlog)->pict_count = which_pict;
  635.     
  636. getout:
  637.     HSetState( text_h, text_state );
  638. }
  639. #endif /* USE_PICTS */
  640.  
  641. /* ---------------------- Scroll_text ---------------------------- */
  642. /*
  643.     This is used as a TrackControl actionProc for scrolling, and also
  644.     called by Auto_scroll for automatic scrolling.
  645. */
  646. static pascal void Scroll_text( ControlHandle the_bar, int part_code )
  647. {
  648.     register TEHandle    the_text;
  649.     register short        delta;
  650.     register WindowPtr    the_display;
  651.     short                old_value;
  652.     short                offset, line;
  653.     Point                place;
  654.     Rect                view;
  655.     TextStyle            style;
  656.     short                line_height, font_ascent;
  657.     
  658.     CKPT( "Scroll_text");
  659. #if A4_PROJECT
  660.     SetUpA4();
  661. #endif
  662.     if (part_code != 0)
  663.     {
  664.         the_display = (**the_bar).contrlOwner;
  665.         the_text = (TEHandle) GetWRefCon( the_display );
  666.         view = (**the_text).viewRect;
  667.         place.h = view.left + TEXT_INSET;
  668.         
  669.         switch (part_code)
  670.         {
  671.             case inUpButton:
  672.                 place.v = view.top - 4;
  673.                 offset = TEGetOffset( place, the_text );
  674.                 place = TEGetPoint( offset, the_text );
  675.                 TEGetStyle( offset, &style, &line_height, &font_ascent,
  676.                     the_text );
  677.                 delta = place.v - line_height - view.top;
  678.                 break;
  679.             case inDownButton:
  680.                 place.v = view.bottom + 2;
  681.                 offset = TEGetOffset( place, the_text );
  682.                 place = TEGetPoint( offset, the_text );
  683.                 /* Now place.v is at the baseline of the border line. */
  684.                 delta = place.v - view.bottom;
  685.                 break;
  686.             case inPageUp:
  687.                 /*
  688.                     I want top border line to remain visible, and
  689.                     the top of a line should end up at view.top.
  690.                 */
  691.                 place.v = view.top + 2;
  692.                 offset = TEGetOffset( place, the_text );
  693.                 place = TEGetPoint( offset, the_text );
  694.                 /* place.v is at the baseline of the top border line. */
  695.                 TEGetStyle( offset, &style, &line_height, &font_ascent,
  696.                     the_text );
  697.                 place.v += line_height - font_ascent;
  698.                 place.v -= view.bottom - view.top;
  699.                 offset = TEGetOffset( place, the_text );
  700.                 place = TEGetPoint( offset, the_text );
  701.                 TEGetStyle( offset, &style, &line_height, &font_ascent,
  702.                     the_text );
  703.                 delta = place.v - view.top;
  704.                 if (offset == 0)
  705.                     delta -= line_height;
  706.                 break;
  707.             case inPageDown:
  708.                 /*
  709.                     I want bottom border line to remain visible, and
  710.                     the bottom of a line should end up at view.bottom.
  711.                 */
  712.                 place.v = view.bottom - 2;
  713.                 offset = TEGetOffset( place, the_text );
  714.                 place = TEGetPoint( offset, the_text );
  715.                 /* place.v is at the baseline of the bottom border line. */
  716.                 TEGetStyle( offset, &style, &line_height, &font_ascent,
  717.                     the_text );
  718.                 place.v -= font_ascent; /* Top edge of bottom border line */
  719.                 place.v += view.bottom - view.top;
  720.                 /* We're looking at the bottom border of the next page. */
  721.                 offset = TEGetOffset( place, the_text );
  722.                 place = TEGetPoint( offset, the_text );
  723.                 TEGetStyle( offset, &style, &line_height, &font_ascent,
  724.                     the_text );
  725.                 delta =  place.v - line_height - view.bottom;
  726.                 if (offset == (**the_text).teLength)
  727.                     delta += line_height;
  728.                 break;
  729.         }
  730.         old_value = GetCtlValue( the_bar );
  731.         if ( ((delta < 0) && (old_value > 0)) ||
  732.             ((delta > 0) && (old_value < GetCtlMax(the_bar))) )
  733.         {
  734.             /*
  735.                 When this routine is called, TextEdit may have set the
  736.                 clipping region to the view rectangle, so we reset it
  737.                 here to make sure the scroll bar gets drawn.
  738.             */
  739.             GetClip( save_clip );
  740.             ClipRect( &the_display->portRect );
  741.             SetCtlValue( the_bar, old_value + delta );
  742.             SetClip( save_clip );
  743.         }
  744.         Adjust_text( the_display );
  745.     }
  746. #if A4_PROJECT
  747.     RestoreA4();
  748. #endif
  749. }
  750.  
  751. /* ---------------------- Adjust_text ---------------------------- */
  752. /*
  753.     Called by Handle_scroll and Scroll_text to scroll the text and
  754.     pictures into sync with the scroll bar's control value.
  755. */
  756. static void Adjust_text( DialogPtr    dialog )
  757. {
  758.     register    TEHandle    the_text;
  759.     register    short    scroll_down;
  760.     short            old_scroll;
  761.     Rect            update_rect;
  762.     ControlHandle    the_bar;
  763.     
  764.     CKPT( "Adjust_text");
  765.     the_text = (TEHandle) GetWRefCon( dialog );
  766.     the_bar = ((help_ptr) dialog)->scrollbar;
  767.     old_scroll = (**the_text).viewRect.top - (**the_text).destRect.top;
  768.     scroll_down = old_scroll - GetCtlValue( the_bar );
  769.     if (scroll_down == 0)
  770.         return;
  771. #if USE_PICTS
  772.     ((help_ptr) dialog)->high_defer_flag = true;
  773.     ((help_ptr) dialog)->high_waiting = 0;
  774. #endif
  775.     TEScroll( 0, scroll_down, the_text );
  776. #if USE_PICTS
  777.     update_rect = (**the_text).viewRect;
  778.     if (scroll_down > 0)
  779.     {
  780.         if (scroll_down < (update_rect.bottom - update_rect.top))
  781.             update_rect.bottom = update_rect.top + scroll_down;
  782.     }
  783.     else
  784.         if (- scroll_down < (update_rect.bottom - update_rect.top))
  785.             update_rect.top = update_rect.bottom + scroll_down;
  786.     Draw_picts( dialog, &update_rect );
  787.     Do_deferred_hilites( (help_ptr) dialog, &update_rect );
  788. #endif
  789. }
  790.  
  791. #if USE_PICTS
  792. /* ---------------------- Do_deferred_hilites ---------------------- */
  793. static void Do_deferred_hilites( help_ptr  hptr, Rect *update_rect )
  794. {
  795.     Rect            *hilite;
  796.     
  797.     while (hptr->high_waiting > 0)
  798.     {
  799.         hptr->high_waiting--;
  800.         hilite =
  801.             &hptr->high_rect[hptr->high_waiting];
  802.         if (SectRect( hilite, update_rect, hilite ))
  803.             InvertRect( hilite );
  804.     }
  805.     hptr->high_defer_flag = false;
  806. }
  807. #endif
  808.  
  809. /* ---------------------- Handle_scroll ---------------------------- */
  810. /*
  811.     Called by Help_filter to handle mouseDown events in the scroll bar.
  812. */
  813. static void Handle_scroll( DialogPtr dialog, short the_part, Point where )
  814. {
  815.     register    ControlHandle the_bar;
  816.     
  817.     CKPT( "Handle_scroll"); SetPort( dialog );
  818.     the_bar = ((help_ptr) dialog)->scrollbar;
  819.     if (the_part == inThumb)
  820.     {
  821.         (void) TrackControl( the_bar, where, NIL );
  822.         Adjust_text( dialog );
  823.     }
  824.     else
  825.         (void) TrackControl( the_bar, where, (ProcPtr)Scroll_text );
  826.     
  827. }
  828.  
  829. /* ---------------------- Help_filter ------------------------- */
  830. /*
  831.     This is the dialog event filter for our help window.
  832. */
  833. #define RETURN_KEY    0x24
  834. #define ENTER_KEY    0x4C
  835. #define TILDE_KEY    0x32
  836. #define ESCAPE_KEY    0x35
  837.  
  838.  
  839. static pascal Boolean Help_filter( DialogPtr dialog,
  840.     EventRecord    *event, short *itemHit)
  841. {
  842.     Point    local_point;
  843.     short    the_part;
  844.     ControlHandle    the_control;
  845.     short    keycode, charcode;
  846.     char    the_char;
  847.     register TEHandle    the_text;
  848.     Rect    item_box;
  849.     short    cursor;
  850.     
  851. #if A4_PROJECT
  852.     SetUpA4();
  853. #endif
  854.     the_text = (TEHandle) GetWRefCon( dialog );
  855.     GetMouse( &local_point );
  856.     if (PtInRect( local_point, &(**the_text).viewRect ))
  857.         SetCursor( *ibeam_cursor );
  858.     else
  859.         InitCursor();
  860.     TEIdle( the_text );
  861.     switch (event->what) {
  862.         case nullEvent:
  863.             break;
  864.         case mouseDown:
  865.             CKPT( "Help_filter mousedown");
  866.             local_point = event->where;
  867.             GlobalToLocal( &local_point );
  868.             the_part = FindControl( local_point, dialog, &the_control );
  869.             if (the_part && ((**the_control).contrlMax > 1) )
  870.             {
  871.                 Handle_scroll( dialog, the_part, local_point );    
  872.                 *itemHit = 2;
  873.                 break;
  874.             }
  875.             if (PtInRect( local_point, &(**the_text).viewRect ))
  876.             {
  877.                 if (event->modifiers & shiftKey)
  878.                     TEClick( local_point, true, the_text );
  879.                 else
  880.                     TEClick( local_point, false, the_text );
  881.             }
  882.             break;
  883.         case keyDown :    
  884.         case autoKey :
  885.             keycode = (event->message & keyCodeMask) >> 8;
  886.             charcode = event->message & charCodeMask;
  887.             /*
  888.                 There's no Cancel button, so we treat the OK button
  889.                 the same as a Cancel button.
  890.             */
  891.             if ( (keycode == RETURN_KEY) || (keycode == ENTER_KEY) ||
  892.                 (keycode == TILDE_KEY) || (keycode == ESCAPE_KEY) ||
  893.                 ((charcode == '.') && (event->modifiers & cmdKey)) )
  894.             {
  895.                 *itemHit = c_OK;    /* OK */
  896.                 Flash_button( dialog, *itemHit );
  897. #if A4_PROJECT
  898.                 RestoreA4();
  899. #endif
  900.                 return( TRUE );
  901.             }
  902.             if ( (charcode == 'c') && (event->modifiers & cmdKey) )
  903.             {
  904.                 (void) ZeroScrap();
  905.                 TECopy( the_text );
  906.                 SystemEdit(3);
  907.                 local_point = (**the_text).selPoint;
  908.                 *(long *)&local_point = PinRect( &(**the_text).viewRect,
  909.                     local_point );
  910.                 cursor = TEGetOffset( local_point, the_text );
  911.                 TESetSelect( (long)cursor, (long)cursor, the_text );
  912.                 event->what = nullEvent;
  913.             }
  914.             break;
  915.     } /* end switch */
  916.     
  917.     /* tell the Dialog Manager that the event has NOT been handled and that it should
  918.     ** take further action on this event.
  919.     */
  920. #if A4_PROJECT
  921.     RestoreA4();
  922. #endif
  923.     return false;
  924. }
  925.  
  926.  
  927. /* ---------------------- userItem_proc ------------------------- */
  928. static pascal void  userItem_proc( WindowPtr the_window, short item_num )
  929. {
  930.     Handle        item_h;
  931.     Rect        item_box;
  932.     short        item_type;
  933.     TEHandle    the_text;
  934.     
  935.     asm {
  936.         MOVEM.L    a1-a5/d0-d7, -(SP)
  937.     }
  938.     
  939.     CKPT( "UserItem_proc");
  940. #if A4_PROJECT
  941.     SetUpA4();
  942. #endif
  943.     the_text = (TEHandle) GetWRefCon( the_window );
  944.     item_box = (**the_text).viewRect;
  945. #if USE_PICTS
  946.     ((help_ptr) the_window)->high_defer_flag = true;
  947.     ((help_ptr) the_window)->high_waiting = 0;
  948. #endif
  949.     TEUpdate( &item_box, the_text );
  950.     
  951. #if USE_PICTS
  952.     Draw_picts( the_window, &item_box );
  953.     Do_deferred_hilites( (help_ptr) the_window, &item_box );
  954. #endif
  955.  
  956.     /*
  957.         Get the item's rectangle, and frame it.
  958.     */
  959.     GetDItem( the_window, item_num, &item_type, &item_h, &item_box );
  960.     FrameRect( &item_box );
  961.  
  962. #if A4_PROJECT
  963.     RestoreA4();
  964. #endif
  965.     asm {
  966.         movem.l    (SP)+, a1-a5/d0-d7        ; restore registers
  967.     }
  968. }
  969.  
  970. /* ---------------------- Menu_userItem_proc ------------------------- */
  971. static pascal void  Menu_userItem_proc( WindowPtr the_window, short item_num )
  972. {
  973.     Handle        item_h;
  974.     Rect        item_box;
  975.     short        item_type;
  976.     
  977.     asm {
  978.         MOVEM.L    a1-a5/d0-d7, -(SP)
  979.     }
  980.     
  981.     CKPT( "Menu_UserItem_proc");
  982. #if A4_PROJECT
  983.     SetUpA4();
  984. #endif
  985.     
  986.     /*
  987.         Get the item's rectangle, and frame it.
  988.     */
  989.     GetDItem( the_window, item_num, &item_type, &item_h, &item_box );
  990.     InsetRect(&item_box, -1, -1);
  991.     FrameRect( &item_box );
  992.     
  993.     /* Draw the drop-shadow */
  994.     MoveTo( item_box.left + 3, item_box.bottom );
  995.     LineTo( item_box.right, item_box.bottom );
  996.     LineTo( item_box.right, item_box.top + 3 );        
  997.  
  998. #if A4_PROJECT
  999.     RestoreA4();
  1000. #endif
  1001.     asm {
  1002.         movem.l    (SP)+, a1-a5/d0-d7        ; restore registers
  1003.     }
  1004. }
  1005.  
  1006. /* ---------------------- Flash_button ----------------------------- */
  1007. void Flash_button( DialogPtr the_dialog, short item_number )
  1008. {
  1009.     ControlHandle    item_h;
  1010.     long    time;
  1011.     short    itype;
  1012.     Rect    box;
  1013.     
  1014.     GetDItem( the_dialog, item_number, &itype, (Handle *)&item_h, &box );
  1015.     HiliteControl( item_h, inButton );
  1016.     Delay( 9L, &time );
  1017.     HiliteControl( item_h, 0 );
  1018. }
  1019.